<?php

class UserModel extends CI_Model {

	function validate() {
		$this -> db -> where('username', $this -> input -> post('username'));
		$this -> db -> where('password', md5($this -> input -> post('password')));
		$query = $this -> db -> get('blog_user');

		return $query -> row_array();

	}
	
	

	public function getUserById($id = NULL) {

		if ($id != NULL) {
			$this -> db -> where('id', $id);
			$query = $this -> db -> get('blog_user');
			return $query -> row_array();
		} 
	}

	public function getUsers($per_page = NULL, $offset = NULL) {

		$query = $this -> db -> get('blog_user', $per_page, $offset);
		return $query -> result_array();

	}

	public function search($term = "") {

		if ($term != NULL && strlen($term)) {

			$array = array('first_name' => $term);

			$this -> db -> like($array);

			$query = $this -> db -> get('blog_user');

			return $query -> result_array();

		}
	}

	public function add($data) {

		/**
		 * clear not necessary data.
		 * */
		unset($data['password2']);
		unset($data['submit']);

		/**
		 * Adding the date
		 * */

		$data['date_created'] = date('Y-m-d H:i:s');
		$data['password'] = md5($data['password']);

		$this -> db -> insert('blog_user', $data);
		return $this -> db -> insert_id();
	}

	public function updaterecord($data, $id) {

		$id = (int)$id;
		if (empty($data['password'])) {
			unset($data['password']);
		} else {
			$data['password'] = md5($data['password']);
		}
		unset($data['password2']);
		unset($data['submit']);

		$this -> db -> where('id', $id);
		return $this -> db -> update('blog_user', $data) ? TRUE : FALSE;

	}

	public function delete_record($id) {
		$this -> db -> where('id', $id);
		$this -> db -> delete('blog_user');
	}

}
